package cn.neusoft.controller;

import cn.neusoft.domain.Order1;
import cn.neusoft.domain.Task;
import cn.neusoft.domain.Warehouse;
import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.feignClients.AdminFeignClient;
import cn.neusoft.feignClients.OrderFeignClient;
import cn.neusoft.query.OrderQuery;
import cn.neusoft.query.TaskQuery;
import cn.neusoft.service.TaskService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController // @Controller + @ResponseBody
@RequestMapping(value = "/task",produces = "application/json;charset=utf-8")
//接口文档的注解
@Api(value = "任务单模块的API",description="任务单相关的CRUD功能")
public class TaskController {
    @Autowired
    TaskService taskService;

    @Autowired
    OrderFeignClient orderFeignClient;

    @Autowired
    AdminFeignClient adminFeignClient;

    @PostMapping
    @ApiOperation(value = "新增任务单")
    public ResultBean add(@RequestBody Task task) {

        System.out.println(task);

        if (task == null
                || StringUtils.isEmpty(task.getOrder_id())
                || task.getSubstation_id() == null
                || task.getWarehouse_id() == null
                || task.getDelivery_fee() == null
        ) {

            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        String task_id = taskService.add(task);

        return ResultBean.success("success", task_id);
    }

    @PutMapping
    @ApiOperation(value = "修改任务单")
    public ResultBean update(@RequestBody Task task) {

        if (task == null
                || StringUtils.isEmpty(task.getTask_id())
                || task.getSubstation_id() == null
                || task.getDelivery_fee() == null
        ) {

            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        taskService.update(task);

        return ResultBean.success();
    }

    @PostMapping("/page")
    @ApiOperation("分页模糊查询任务单信息")
    public ResultBean findByPage(@RequestBody TaskQuery taskQuery) {

        if(taskQuery == null
                || StringUtils.isEmpty(taskQuery.getPageNow())
                || StringUtils.isEmpty(taskQuery.getPageSize())
        ){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        PageBean<Task> pageBean = taskService.findByPage(taskQuery);

        return ResultBean.success(pageBean);
    }

    @GetMapping("/{task_id}")
    @ApiOperation("根据id查询任务单信息")
    public ResultBean<Task> findById(@PathVariable("task_id") String task_id) {

        if(StringUtils.isEmpty(task_id)){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        Task task = taskService.findById(task_id);

        if (task == null){
            throw new AppException(AppExceptionCodeMsg.TASK_NOT_EXISTS);
        }

        return ResultBean.success(task);
    }


    @PutMapping("/updateTask")
    @ApiOperation(value = "修改任务单(系统内自用)")
    public ResultBean updateTask(@RequestBody Task task) {

        if (task == null
                || StringUtils.isEmpty(task.getTask_id())
        ) {

            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        taskService.updateTask(task);

        return ResultBean.success();
    }

    @PutMapping("/entryTask")
    @ApiOperation(value = "回执任务单")
    public ResultBean entryTask(@RequestBody Task task) {

        if (task == null
                || StringUtils.isEmpty(task.getTask_id())
                || StringUtils.isEmpty(task.getRemark())
                || task.getTask_state() == null
        ) {

            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        taskService.entryTask(task);

        return ResultBean.success();
    }

    @PostMapping("/findOrderByCondition")
    @ApiOperation(value = "查询订单信息")
    public ResultBean<Order1> findOrderByCondition(@RequestBody OrderQuery orderQuery){

        // 通过openfeign向客服模块发送请求，得到订单信息
        ResultBean<Order1> resultBean = orderFeignClient.findOrderByCondition(orderQuery);

        return resultBean;
    }

    @PostMapping("/findOrderByPage")
    @ApiOperation(value = "分页查询订单信息")
    public ResultBean findOrderByPage(@RequestBody OrderQuery orderQuery){

        // 通过openfeign向客服模块发送请求，得到订单信息
        ResultBean resultBean = orderFeignClient.findOrderByPage(orderQuery);

        return resultBean;
    }

    @GetMapping("/normalSubstationTreeSelect")
    @ApiOperation(value = "得到正常分站级联下拉列表")
    public ResultBean getNormalSubstationTreeSelect(){

        // 通过openfeign向管理员模块发送请求，得到分站级联下拉列表
        ResultBean resultBean = adminFeignClient.getNormalSubstationTreeSelect();

        return resultBean;
    }

    @GetMapping("/normalWarehouseTreeSelect")
    @ApiOperation(value = "得到正常仓库级联下拉列表")
    public ResultBean getNormalWarehouseTreeSelect(){

        // 通过openfeign向管理员模块发送请求，得到仓库级联下拉列表
        ResultBean resultBean = adminFeignClient.getNormalWarehouseTreeSelect();


        return resultBean;
    }

    @PostMapping("/scheduling")
    @ApiOperation(value = "调度")
    public ResultBean<Order1> scheduling(@RequestBody OrderQuery orderQuery) throws Exception {

        if(orderQuery.getOrder_kind() == null || orderQuery.getOrder_id() == null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        Map<String, Object> resultData = taskService.scheduling(orderQuery);

        return ResultBean.success(resultData);
    }

    @GetMapping("/findWarehouseBySubstationId/{substation_id}")
    @ApiOperation(value = "根据分站id找到其父库房")
    public ResultBean findWarehouseBySubstationId(@PathVariable("substation_id") Long substation_id){

        if (substation_id == null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        Warehouse warehouse = taskService.findWarehouseBySubstationId(substation_id);

        return ResultBean.success(warehouse);
    }

    @GetMapping("/findFittingSubstation/{amount}")
    @ApiOperation(value = "找到合适的分站")
    public ResultBean findFittingSubstation(@PathVariable("amount") Long amount){

        if (amount == null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        List<Map<String, Object>> substationSelect = taskService.findFittingSubstation(amount);

        return ResultBean.success(substationSelect);
    }

    @PutMapping("/updateStrategy/{strategy}")
    @ApiOperation(value = "更改调度策略")
    public ResultBean updateStrategy(@PathVariable("strategy") Integer strategy){

        if (strategy == null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        taskService.updateStrategy(strategy);

        return ResultBean.success();
    }

    @GetMapping("/getStrategy")
    @ApiOperation(value = "得到当前调度策略")
    public ResultBean getStrategy(){

        Integer strategy = taskService.getStrategy();

        return ResultBean.success("成功", String.valueOf(strategy));
    }

}
